@file:OptIn(ExperimentalFoundationApi::class)

package toy.keli.edic.ui.home.homescreen

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import toy.keli.edic.data.UserStatus
import toy.keli.edic.data.config.PageConf
import toy.keli.edic.domain.model.WordModel
import toy.keli.edic.ui.history.HistoryWords
import toy.keli.edic.ui.home.HomeEvents
import toy.keli.edic.ui.home.WordModelViewModel
import toy.keli.edic.ui.home.components.SpeakButton

@Composable
fun DefaultPageWordTitle(wordViewModel:WordModelViewModel) {
    val wordModelState by wordViewModel.wordState.collectAsState()
    val wordModel = wordModelState.wordModel

    val wordCanEdit = UserStatus().getMutableStatus("wordCanEdit")
    val isEdit =  (wordCanEdit.value==1)
    val word = wordModel?.word ?: "";
    val ch = wordModel?.ch
    Row(modifier = Modifier.fillMaxWidth()
        .padding(32.dp, 32.dp, end = 32.dp, bottom = 16.dp)
        ,horizontalArrangement = Arrangement.Center, // 水平居中
        verticalAlignment = Alignment.CenterVertically // 垂直居中
    ){

            if (!isEdit) {
                Text(
                    text = word,
                    style = TextStyle(
                        fontWeight = FontWeight.Bold,
                        fontSize = 48.sp,
                        letterSpacing = 0.15.sp
                    ),
                    color = MaterialTheme.colorScheme.onSurface,
                    modifier = Modifier
                        .combinedClickable(
                            onLongClick = {
                                //长按如果支持编辑则编辑 否则则搜索
                                if (PageConf.getBoolean(PageConf.homePage.TitleClickEdit, true)) {
                                    wordCanEdit.value = 1
                                } else {
                                    HomeEvents.searchTools.show(word);
                                }
                            },
                            onClick = {
                                HomeEvents.searchTools.show(word);
                            },
                            onDoubleClick = {
                                if (HistoryWords.size > 1) {
                                    HistoryWords.menu.show { _, v ->
                                        wordViewModel.searcher(v.name)
                                        HistoryWords.slice(v.index)
                                    }
                                }
                            }
                        )
                )
                HistoryWords.menu()
            } else {
                wordViewModel.searchByText()
                if (word != null && ch != null) {
                    TitleEdit(word, ch) { it ->
                        wordCanEdit.value = if (it) 1 else 0
                        if (isEdit) {
                            wordModel?.let {
                                it.status = WordModel.STATUS_KNOW
                                wordViewModel.insertHistory(it)
                                onOpenNextWord()
                            }
                        }
                    }
                }
            }
        Spacer(modifier = Modifier.width(10.dp))
        SpeakButton(word=wordModel?.word?:"", hasText = false)
        }
}